package cn.com.fuhuitong.main.access.ui.fragment

import cn.com.fuhuitong.app.WX_APP_ID
import cn.com.fuhuitong.base.BaseAbstractFragment
import com.jakewharton.rxbinding2.widget.RxTextView
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.entity.WebConfig
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPStaticUtils
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.BiFunction
import kotlinx.android.synthetic.main.fragment_access_account.*

class AccessAccountFragment : BaseAbstractFragment() {

    private val dispose: CompositeDisposable = CompositeDisposable()

    companion object {
        fun newInstance(): AccessAccountFragment {
            return AccessAccountFragment()
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_account

    override fun initView() {

        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(edit_account_phone)
        val observablePassword: Observable<CharSequence> =
            RxTextView.textChanges(edit_account_password)
        val observable: Disposable = Observable.combineLatest(observablePhone, observablePassword,
            BiFunction<CharSequence, CharSequence, Boolean> { t1, t2 ->
                t1.isNotEmpty() && t2.isNotEmpty()
            })
            .subscribe {
                text_account_login.isEnabled = it
            }
        dispose.add(observable)

        //忘记密码
        text_account_pwd_forget.setOnClickListener {
            requireFragmentManager().beginTransaction()
                .add(R.id.frameLayout_access, AccessChangeFragment.newInstance())
                .addToBackStack("")
                .commit()
        }
        //马上注册
        text_account_register_now.setOnClickListener {
            requireFragmentManager().beginTransaction()
                .add(R.id.frameLayout_access, AccessRegisterFragment.newInstance())
                .addToBackStack("")
                .commit()
        }
        //快捷登录
        text_account_login_quick.setOnClickListener {
            requireFragmentManager().beginTransaction()
                .add(R.id.frameLayout_access, AccessMobileFragment.newInstance())
                .addToBackStack("")
                .commit()
        }
        //微信登录
        text_account_login_wechat.setOnClickListener {
            val msgApi = WXAPIFactory.createWXAPI(requireContext(), null)
            if (!msgApi.isWXAppInstalled) {
                toast("未安装微信")
            } else {
                msgApi.registerApp(WX_APP_ID)
                msgApi.sendReq(SendAuth.Req().apply {
                    scope = "snsapi_userinfo"
                    state = "auth"
                })
            }
        }
//        //用户协议
//        text_account_agree_user.setOnClickListener {
//            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_WEB_VIEW)
//                .withSerializable(
//                    AppConstance.BUNDLE_WEB_VIEW,
//                    MainBean(type = WebConfig.USER_AGREE, data = null)
//                )
//                .navigation()
//        }
//        //隐私协议
//        text_account_agree_hide.setOnClickListener {
//            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_WEB_VIEW)
//                .withSerializable(
//                    AppConstance.BUNDLE_WEB_VIEW,
//                    MainBean(type = WebConfig.PRIVACY_AGREE, data = null)
//                )
//                .navigation()
//        }
        check_account_select.setOnCheckedChangeListener { buttonView, isChecked ->
            SPStaticUtils.put(AppConstance.BUNDLE_USER_AGREE, isChecked)
        }

        check_account_select.isChecked =
            SPStaticUtils.getBoolean(AppConstance.BUNDLE_USER_AGREE, (false))
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (dispose.size() > 0 && !dispose.isDisposed) {
            dispose.dispose()
        }
    }
}